<?php

class BooksController  extends Zend_Controller_Action{
    public function init(){

    }

    public function indexAction(){
        $filterBooks = new Application_Form_FilterBook();
        $filterBooks->isValid($this->getRequest()->getParams());

        $booksPaginator = Application_Model_Books::getInstance()
            ->getPaginatorForFilter($filterBooks->getValues());
        $booksPaginator->setItemCountPerPage(12);
        $page = $this->getRequest()->getParam('page');
        if(!is_null($page)){
            $booksPaginator->setCurrentPageNumber($page);
        }
        $this->view->filterBooks = $filterBooks;
        $this->view->filterParams = $this->getRequest()->getParams();
        $this->view->booksPaginator = $booksPaginator;
    }

    public function addAction(){
        $formBook = new Application_Form_Book();
        $request = $this->getRequest();
        if($request->isPost()){
            if($formBook->isValid($request->getPost())){
                $bookValues = $formBook->getValues();
                $bookValues['book_number_of_free_copies'] = $bookValues['book_number_of_copies'];
                Application_Model_Books::getInstance()->add($bookValues);
                $this->redirect($this->view->url(array('controller' => 'books'), null, true));
            }
        }
        $this->view->formBook = $formBook;
    }
}